﻿using FinalProject.Data.Model;
using FinalProjectMVC.Adapters.Adapters;
using FinalProjectMVC.Adapters.Interfaces;
using FinalProjectMVC.Models;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;

using System.Net.Mail;

using System.Threading.Tasks;
using System.Web;

using System.Web.Mail;

using System.Web.Mvc;

namespace FinalProjectMVC.Controllers
{
    public class HomeController : Controller
    {
        private IPhotosAdapter _adapter;

        public HomeController()
        {
            _adapter = new PhotosAdapter();
        }

        public HomeController(IPhotosAdapter a)
        {
            _adapter = a;
        }

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = Roles.ADMIN)]
        public ActionResult Pictures()
        {
            List<PhotosVM> Photos = _adapter.GetAllPhotos();
            return View(Photos);
        }

        public ActionResult Events()
        {
            List<EventVM> Events = _adapter.GetAllEvents();
            return View(Events);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        //Make this PhotoEdit
        public ActionResult PhotoCRUD(int id)
        {
            PhotosVM model = _adapter.CRUDForPhoto(id);
            return View(model);
        }

        [Authorize]
        public async Task<ActionResult> Contact(ApplicationUser user)
        {
            var body = "Thank you for registering with The Shannon Fan Club.  You will receve emails to keep you updated with Shannon";
            var message = new System.Net.Mail.MailMessage();
            message.To.Add(new MailAddress(user.Email));
            message.From = new MailAddress("testapplicationemail1237@gmail.com");
            message.Subject = "Welcome To Fan Club";
            message.Body = string.Format(body, user.UserName, user.Email);
            message.IsBodyHtml = false;

            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = "testapplicationemail1237@gmail.com",
                    Password = "12345612312",
                };
                smtp.Credentials = credential;
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                await smtp.SendMailAsync(message);
                return RedirectToAction("Index");
            }
        }

        [Authorize(Roles = Roles.ADMIN)]
        public ActionResult AddPhoto()
        {
            PhotosVM photo = new PhotosVM();
            return View(photo);
        }

        [HttpPost]
        public ActionResult AddPhoto(PhotosVM photo)
        {
            if (ModelState.IsValid)
            {
                _adapter.AddPhoto(photo);
            }
            return RedirectToAction("Index");
        }

        public ActionResult PhotoEdit(int id)
        {
            PhotosVM photo = _adapter.GetPhoto(id);
            return View("AddPhoto", photo);
        }

        [HttpPost]
        public ActionResult PhotoEdit(PhotosVM photo)
        {
            if (ModelState.IsValid)
            {
                _adapter.EditPhoto(photo);
                return RedirectToAction("Index");
            }
            return View(photo);
        }

        public ActionResult PhotoDelete(int id)
        {
            _adapter.DeletePhoto(id);
            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult CreateComment(int id)
        {
            CommentsVM comment = _adapter.GetComment(id);
            return View(comment);
        }

        [HttpPost]
        public ActionResult CreateComment(CommentsVM model)
        {
            model.UserId = User.Identity.GetUserId();
            _adapter.CreateComment(model);
            return View();
        }
    }
}